//
//  KYCustomCheckInShowView.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/10/18.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "KYCustomCheckInShowView.h"
#import "KYCheckInCommon.h"
#import "UIImageView+KYWebCache.h"
#import "KYCheckInNetDateModel.h"
#import "KYAdViewController.h"
#import "KYServer.h"
#import "KYCalendarCellItem.h"
#import "KYCheckInPrizeDetialModel.h"


@interface KYCustomCheckInShowView()
/**
 *  关闭图片
 */
@property (nonatomic, weak) UIButton *cancelBtn;
/**
 *  背景
 */
@property (nonatomic, weak) UIView *bgView;
/**
 *  展现内容的view
 */
@property (nonatomic, weak) UIView *contentView;

/**
 *  添加金币的view
 */
@property (nonatomic, weak) UIView *headerView;
/**
 *  金币的图片
 */
@property (nonatomic, weak) UIImageView *coinImgView;
/**
 *  添加金币的Label
 */
@property (nonatomic, weak) UILabel *tipsLabel;
/**
 *  签到描述
 */
@property (nonatomic, weak) UILabel *detailLabel;
/**
 *  福利图片
 */
@property (nonatomic, weak) UIImageView *wellImageView;
/**
 *  福利提示
 */
@property (nonatomic, weak) UILabel *wellLabel;
/**
 *  领取福利的背景
 */
@property (nonatomic, weak) UIImageView *getWellBgView;
/**
 *  确定
 */
@property (nonatomic, weak) UIButton *getWellBtn;

//@property (nonatomic, assign)NSInteger coinCount;

/**
 *  背景图片
 */
@property (nonatomic, weak) UIImageView *backgroundImgview;

/**
 *  亮光图片
 */
@property (nonatomic, weak) UIImageView *lightImgview;

/**
 广告图片
 */
@property (nonatomic, weak) UIImageView *adImageView;

/**
 广告文字
 */
@property (nonatomic, weak) UILabel *adLabel;

/**
 广告文字背景view
 */
@property (nonatomic, weak) UIView *adLabelBgView;
@end

@implementation KYCustomCheckInShowView

+ (instancetype)customCheckInShowView {
    static KYCustomCheckInShowView *item = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        item = [[self alloc] init];
    });
    return item;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setUpSubViews];
    }
    
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)decoder {
    if (self = [super initWithCoder:decoder]) {
        [self setUpSubViews];
    }
    
    return self;
}

- (void)setUpSubViews
{
    // 0.初始化子控件
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor blackColor];
    bgView.alpha = 0.65;
    [self addSubview:bgView];
    self.bgView = bgView;
    
    //背景亮光view
    UIImageView *lightImgView = [[UIImageView alloc] init];
    lightImgView.image = [UIImage ky_imageNamed:@"checkIn_guang"];
    [self addSubview:lightImgView];
    self.lightImgview = lightImgView;
    
    
    // 2.背景图片
    UIImageView *backgroundImgview = [[UIImageView alloc] init];
    backgroundImgview.image = [UIImage ky_imageNamed:@"checkIn_tanChuang"];
    [self addSubview:backgroundImgview];
    self.backgroundImgview = backgroundImgview;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(adImgTap)];
    self.backgroundImgview.userInteractionEnabled = YES;
    [self.backgroundImgview addGestureRecognizer:tap];
    
    // 1.关闭按钮
    UIButton *cancelBtn = [[UIButton alloc] init];
    [cancelBtn setImage:[UIImage ky_imageNamed:@"checkIn_close"] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
    cancelBtn.tag = KYCustomCheckInShowViewCancelBtnType;
    [self addSubview:cancelBtn];
    self.cancelBtn = cancelBtn;
    
    //3.广告图片
    UIImageView *adImageView = [[UIImageView alloc] init];
    [self.backgroundImgview addSubview:adImageView];
    self.adImageView = adImageView;
    
    //4.广告文字背景View
    UIView *adLabelbgView = [[UIView alloc] init];
    adLabelbgView.backgroundColor = KYColor(209, 0, 0, 1);
    adLabelbgView.layer.cornerRadius = 14;
    adLabelbgView.layer.masksToBounds = YES;
    [self.backgroundImgview addSubview:adLabelbgView];
    self.adLabelBgView = adLabelbgView;
    
    //5.广告文字
    UILabel *adLabel = [[UILabel alloc] init];
    adLabel.font = [UIFont systemFontOfSize:18];
    adLabel.textColor = [UIColor whiteColor];
    adLabel.text = @"30元淘宝优惠券";
    adLabel.textAlignment = NSTextAlignmentCenter;
    [adLabel sizeToFit];
    [self.backgroundImgview addSubview:adLabel];
    self.adLabel = adLabel;
}

#pragma mark - method
- (void)buttonClick:(UIButton *)btn
{
    if (btn.tag == KYCustomCheckInShowViewCancelBtnType) {
        [self hide];
    } else {
        
    }
}

- (void)adImgTap{
    [self hide];
    if (self.adViewClick) self.adViewClick();
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    self.bgView.frame = self.bounds;
    
    //亮光
    CGFloat lightW = 300;
    CGFloat lightH = 300;
    self.lightImgview.center = self.bgView.center;
    self.lightImgview.ky_centerY -= 80;
    self.lightImgview.bounds = CGRectMake(0, 0, lightW, lightH);
    
    //背景图
    CGFloat bgImageViewW = kScreenWidth - 20;
    CGFloat bgImageViewH = bgImageViewW*1136/996;
    self.backgroundImgview.center = self.bgView.center;
    self.backgroundImgview.ky_centerY += 60;
    self.backgroundImgview.bounds = CGRectMake(0, 0, bgImageViewW, bgImageViewH);
    
    //关闭按钮
    CGFloat cancelBtnW = 30;
    CGFloat cancelBtnX = CGRectGetMaxX(self.backgroundImgview.frame) - cancelBtnW - 15;
    CGFloat cancelBtnY = self.backgroundImgview.ky_top - cancelBtnW - 20;
    CGFloat cancelBtnH = cancelBtnW;
    self.cancelBtn.frame = CGRectMake(cancelBtnX, cancelBtnY, cancelBtnW, cancelBtnH);
    
    //广告图
    CGFloat adImgViewW = bgImageViewW - 60;
    CGFloat adImgViewH = adImgViewW * 300 / 460;
    self.adImageView.bounds = CGRectMake(0, 0, adImgViewW, adImgViewH);
    self.adImageView.center = CGPointMake(bgImageViewW/2, 10+adImgViewH/2);
    
    //广告文字
    self.adLabel.ky_left = (self.backgroundImgview.ky_width-self.adLabel.ky_width)/2;
    self.adLabel.ky_top = self.adImageView.ky_bottom + 10;
    
    //广告文字背景view
    CGFloat labelBgViewW = self.adLabel.ky_width + 40;
    CGFloat labelBgViewH = 30;
    self.adLabelBgView.bounds = CGRectMake(0, 0, labelBgViewW, labelBgViewH);
    self.adLabelBgView.ky_left = (self.backgroundImgview.ky_width-self.adLabelBgView.ky_width)/2;
    self.adLabelBgView.ky_top = self.adImageView.ky_bottom + 6;
}

#pragma mark - public
- (void)refreshDataWithModel:(KYCheckInPrizeDetialModel *)model{
    //统计PV数据
    [KYServer postAdViewClickEvent:@"view" urlString:model.url DataSuccess:nil failure:nil];
    [self.adImageView KYSD_setImageWithURL:[NSURL URLWithString:model.imageurl]];
    self.adLabel.text = model.headline;
    [self.adLabel sizeToFit];
    //广告文字
    self.adLabel.ky_left = (self.backgroundImgview.ky_width-self.adLabel.ky_width)/2;
    self.adLabel.ky_top = self.adImageView.ky_bottom + 10;
    
    //广告文字背景view
    CGFloat labelBgViewW = self.adLabel.ky_width + 40;
    CGFloat labelBgViewH = 30;
    self.adLabelBgView.bounds = CGRectMake(0, 0, labelBgViewW, labelBgViewH);
    self.adLabelBgView.ky_left = (self.backgroundImgview.ky_width-self.adLabelBgView.ky_width)/2;
    self.adLabelBgView.ky_top = self.adImageView.ky_bottom + 6;
}

/**
 *  显示
 */
- (void)show {
    [[[UIApplication sharedApplication].delegate window] addSubview:self];
}

/**
 *  隐藏
 */
- (void)hide {
    [self removeFromSuperview];
}

@end
